package com.settlement.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.settlement.system.common.constant.SystemConstants;
import com.settlement.system.model.entity.SysMainSubNoConfig;
import com.settlement.system.mapper.SysMainSubNoConfigMapper;
import com.settlement.system.service.SysMainSubNoConfigService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.settlement.system.model.query.SysMainSubNoConfigQuery;
import com.settlement.system.model.form.SysMainSubNoConfigForm;
import cn.hutool.core.bean.BeanUtil;
import com.settlement.system.model.vo.SysMainSubNoConfigImportVo;

/**
 * <p>
 * 平台主子单号配置 服务实现类
 * </p>
 *
 * @author huangrensen
 * @since 2024-05-05
 */
@Service
@RequiredArgsConstructor
public class SysMainSubNoConfigServiceImpl extends ServiceImpl<SysMainSubNoConfigMapper, SysMainSubNoConfig> implements SysMainSubNoConfigService {

    private final RedisTemplate redisTemplate;

    @Override
    public Page<SysMainSubNoConfig> getPage(SysMainSubNoConfigQuery queryParams) {
        // 查询参数
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        // 查询数据
        Page<SysMainSubNoConfig> page = new Page<>(pageNum, pageSize);
        return this.baseMapper.getPage(page, queryParams);
    }

    @Override
    public boolean saveSysMainSubNoConfig(SysMainSubNoConfigForm sysMainSubNoConfigForm) {
        // 删除缓存
        redisTemplate.delete(SystemConstants.SYS_MAIN_SUB_NO_CONFIG + sysMainSubNoConfigForm.getPreform());
        // 删除旧的数据
        this.remove(new LambdaQueryWrapper<SysMainSubNoConfig>().eq(SysMainSubNoConfig::getPreform, sysMainSubNoConfigForm.getPreform()));
        SysMainSubNoConfig sysMainSubNoConfig = new SysMainSubNoConfig();
        BeanUtil.copyProperties(sysMainSubNoConfigForm, sysMainSubNoConfig);
        return this.saveOrUpdate(sysMainSubNoConfig);
    }

    @Override
    public SysMainSubNoConfigForm getSysMainSubNoConfigFormData(String preformKey) {
        // 先从缓存取
        SysMainSubNoConfigForm sysMainSubNoConfigForm = (SysMainSubNoConfigForm) redisTemplate.opsForValue().get(SystemConstants.SYS_MAIN_SUB_NO_CONFIG + preformKey);
        if(sysMainSubNoConfigForm != null){
            return sysMainSubNoConfigForm;
        }
        SysMainSubNoConfig sysMainSubNoConfig = this.getOne(new LambdaQueryWrapper<SysMainSubNoConfig>().eq(SysMainSubNoConfig::getPreform, preformKey));
        sysMainSubNoConfigForm = new SysMainSubNoConfigForm();
        BeanUtil.copyProperties(sysMainSubNoConfig, sysMainSubNoConfigForm);
        return sysMainSubNoConfigForm;
    }

    @Override
    public boolean deleteSysMainSubNoConfig(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的数据为空");
        List<Long> idArray = Arrays.stream(ids.split(",")).map(Long::parseLong).collect(Collectors.toList());
        return this.removeByIds(idArray);
    }

    @Override
    public void importData(List<SysMainSubNoConfigImportVo> voList) {
        List<SysMainSubNoConfig> sysMainSubNoConfigList = new ArrayList<>();
        for (SysMainSubNoConfigImportVo importVo : voList) {
            SysMainSubNoConfig sysMainSubNoConfig = new SysMainSubNoConfig();
            BeanUtil.copyProperties(importVo, sysMainSubNoConfig);
            sysMainSubNoConfigList.add(sysMainSubNoConfig);
        }
        this.saveOrUpdateBatch(sysMainSubNoConfigList);
    }

    @Override
    public List<SysMainSubNoConfigImportVo> findByIds(String idsStr) {
        if (StrUtil.isBlank(idsStr)) {
            return new ArrayList<>();
        }
        List<Long> ids = Arrays.stream(idsStr.split(",")).map(Long::parseLong).collect(Collectors.toList());
        List<SysMainSubNoConfig> sysMainSubNoConfigList = this.getBaseMapper().selectBatchIds(ids);
        List<SysMainSubNoConfigImportVo> voList = new ArrayList<>();
        for (SysMainSubNoConfig sysMainSubNoConfig : sysMainSubNoConfigList) {
            SysMainSubNoConfigImportVo vo = new SysMainSubNoConfigImportVo();
            BeanUtil.copyProperties(sysMainSubNoConfig, vo);
            voList.add(vo);
        }
        return voList;
    }
}
